package com.itheima.ThreadSafety;

public class ThreadSafety extends Thread {
    // 这一步是关键；我怎么再线程类里传进来对象
    private Account acc;

    public ThreadSafety(String name, Account acc) {
        super(name);
        this.acc = acc;
    }
    @Override
    public void run() {
        //取钱,最好是写个方法
        acc.withdraw(acc,10000.0);

    }
}

class Test {
    public static void main(String[] args) {
        Account account = new Account(10000.0);
        Thread t1 = new ThreadSafety("小红线程",account);
        Thread t2 = new ThreadSafety("小明线程",account);
        t1.start();
        t2.start();

    }
}

class Account {
    private double money;

    public Account(double money) {
        this.money = money;
    }

    public Account() {
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }
    public void  withdraw(Account acc,double money){
        if(acc.getMoney() >= money) {
            System.out.println(Thread.currentThread().getName()+":取钱:"+money);
            this.money -= money;
            System.out.println(Thread.currentThread().getName()+"余额:"+acc.getMoney());
        }else {
            System.out.println(Thread.currentThread().getName()+"余额不足");
        }
    }
}
